/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.Player;
import entity.User;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import session.manager.UserManagerBeanLocal;
import util.Wrapper;

/**
 *
 * @author Giuseppe
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PasswordForgottenBean implements PasswordForgottenBeanLocal {
    
    @Resource(mappedName = "jms/queue/sendPassword")
    private Queue queueSendPassword;
    
    @Resource(mappedName = "jms/queue/sendPasswordFactory")
    private ConnectionFactory queueSendPasswordFactory;
    
    @EJB
    UserManagerBeanLocal userManager;
    
    @Override
    public boolean sendNewPassword(String username) {
        User user = userManager.find(username);
        if (user == null) {
            return false;
        }
        int BASE = 36;
        int LENGTH = 8;
        String password = Long.toString((long) (Math.pow(BASE, LENGTH) * Math.random()), BASE);
        this.userManager.changePassword(username, password);
        try {
            sendJMSMessageToSendPassword(username, password);
        } catch (JMSException ex) {
            Logger.getLogger(PasswordForgottenBean.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
    
    private Message createJMSMessageForjmsQueueSendPassword(Session session, String username,String password) throws JMSException {
        ObjectMessage tm = session.createObjectMessage();        
        Wrapper messageData=new Wrapper(username,password, 0);
        tm.setObject(messageData);
        return tm;
    }
    
    private void sendJMSMessageToSendPassword(String username,String password) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = queueSendPasswordFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(queueSendPassword);
            messageProducer.send(createJMSMessageForjmsQueueSendPassword(session, username, password));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
    
}
